package com.kzd.gtgame.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.gtgame.app.SPSConstance
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.entity.*
import com.kzd.gtgame.jetpack.manager.*
import com.kzd.gtgame.utils.httpSign1
import com.kzd.gtgame.utils.httpTime
import com.module.lemlin.http.*

class BargainViewModel(application: Application) : BaseViewModel(application) {

    private val mBargainRepository = BargainRepository()


    val bargainDetailsLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<BargainDetails>>>()
    fun bargainDetails(bid: String) {
        setRxLifeScope(SubscribeViewModel(bargainDetailsLiveData)) {
            mBargainRepository.bargainDetails(bid)
        }
    }


    val gamePlayedLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GamePlayed>>>()
    fun bargainGame() {
        setRxLifeScope(SubscribeViewModel(gamePlayedLiveData)) {
            mBargainRepository.bargainGamePlayed()
        }
    }


    val gameSmallLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<GameSmall>>>()
    fun bargainGameSmall(gameId: String, appid: String) {
        setRxLifeScope(SubscribeViewModel(gameSmallLiveData)) {
            mBargainRepository.bargainGameSmall(gameId, appid)
        }
    }

    val bargainSubmitLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun bargainSubmit(
        name: String,
        game_id: String,
        introduction: String,
        sname: String,
        small_id: String,
        two_passwd: String? = null,
        money: String,
        system: String,
        images: String
    ) {
        setRxLifeScope(SubscribeViewModel(bargainSubmitLiveData)) {
            mBargainRepository.bargainSubmit(
                httpTime, httpSign1(SPSConstance.uid), name, game_id, introduction,
                sname, small_id, two_passwd, money, system, images
            )
        }
    }

    private var page: Int = 1

    val bargainRecordLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<BargainRecord>>>()
    fun bargainRecord(type: Int, isLoadMore: Boolean = false) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataE<BargainRecord>>(bargainRecordLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<BargainRecord>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            mBargainRepository.bargainRecord(
                time = httpTime, sign = httpSign1(SPSConstance.uid), page = page.toString(), type = type.toString()
            )
        }
    }

    val bargainPaymentLiveData = MutableLiveData<HttpResponseBody<BargainPaymentResponse>>()
    fun bargainPayment(type: Int, goodsId: String, money: String) {
        setRxLifeScope(object : SubscribeViewModel<BargainPaymentResponse>(bargainPaymentLiveData) {
            override fun onSuccess(resp: BargainPaymentResponse) {
                super.onSuccess(resp.apply {
                    this.type = type
                    this.id = goodsId
                })
                if (type == 1) {
                    userMine()
                }
            }
        }) {
            mBargainRepository.bargainPayment(
                time = httpTime, sign = httpSign1(SPSConstance.uid), type = type.toString(), goodsId = goodsId, money = money
            )
        }
    }
}